#!/usr/bin/python
# -*- coding: cp1250 -*-

from pepe.glib import *
import pepe.p_sqlpg.pg as pgm
import pepe.p_oslevel.basic as osb

def main(sqlf):
    ''' run pg sql file against work db
    '''
    """
    path = fw_slash(osb.wai())
    inputFile = path + sqlf
    inputFileNameOnly = os.path.split(inputFile)[1]
    tableName = inputFileNameOnly[:-4]
    print path, inputFile, inputFileNameOnly, tableName
    """
    u = raw_input("""    Role (default is 'public'): """)
    if u == '':
        username = 'postgres'
    else:
        username = u[:]
    print '\n'
    
    q = open(sqlf, 'r').read()
    
    p = pgm.Pg()

    p.user = username[:]
    p.connect()
    p.query(q)
    
    #for r in p:
    #    print r

if __name__ == "__main__":
    ''' '''
    if  len(sys.argv) == 2:
        main(sys.argv[1])
    """
    if  len(sys.argv) == 3:
        main(sys.argv[1], sys.argv[2])
    """
    if  len(sys.argv) not in (2, 2):
        #sys.stderr.write('usage: pgrun <sql-file> [optional-db (default is work)]')
        sys.stderr.write('usage: pgrun <sql-file>')
